'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 联盟捐增
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var unionTechConf = require('../../../../config/unionTechConf.js');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionPositionType = require('../../../../consts/unionPositionType');
var PlayerManager = require('../../../../libs/playerManager');

/**
 * 设置捐增
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var tech_type = msg.tech_type;
    var player_manager = new PlayerManager(role_id);
    var union_group_model;
    var union_id;
    var donated_list = [];
    var donated_time = 0;

    async.waterfall(
        [
            // function (cb) {
            //     player_manager.refreshData(cb);
            // },
            function (cb) {
                player_manager.getModel(
                ['role', 'unionTask', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                union_id = role_model.getUnionId();
                if (!union_id) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
                unionGroupModel.getById(union_id, cb);
            },
            function (model, cb) {
                union_group_model = model;
                var union_task_model = player_manager.unionTask;
                donated_time = union_task_model.donated_time;
                var tech_id_now = union_group_model.getTechId(tech_type);
                var conf = unionTechConf.getTechConf(tech_id_now);
                if (!conf) {
                    return next(null, {
                        code: Code.UNION_NOT_TECH_ID
                    });
                }
                var reward_type = conf[0].donation_type;
                var reward_num = unionTechConf.getTechTypeConf(1);
                var donated1_reward_type = unionTechConf.getTechConf(tech_id_now)[0].donation_type;
                union_task_model.setDonatedList(tech_type, 1, 1, donated1_reward_type, reward_num);

                var list = union_task_model.donated_list;
                _.some(union_task_model.donated_list, function (donated_item) {
                    if (donated_item.tech_id == tech_type) {
                        donated_list = donated_item.donated_list || [];
                        return true;
                    }
                });
                cb();
            },
            function (cb) {
                player_manager.save(cb);
            },
            function (cb) {
                if (!!union_group_model) {
                    union_group_model.save(cb);
                } else {
                    cb();
                }
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    donated_list: donated_list,
                    donated_time: donated_time
                }
            });
        }
    );
};
